define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yunfood/fund/user/index' + location.search,
                    edit_url: 'yunfood/fund/user/edit',
                    del_url: 'yunfood/fund/user/del',
                    multi_url: 'yunfood/fund/user/multi',
                }
            });

            var table = $("#table");

            // 初始化表格template
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        // 该列为复选框字段,如果后台的返回state值将会默认选中
                        {
                            field: 'state',
                            checkbox: true
                        },
                        // sortable为是否排序,operate为搜索时的操作符,visible表示是否可见
                        {
                            field: 'id',
                            title: 'ID',
                            sortable: true,
                            operate: false
                        },
                        {
                            field: 'number',
                            title: __('编号'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索'
                        },
                        {
                            field: 'user.username',
                            title: __('用户'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索',
                            formatter: function (value, row, index) {
                                return row.user_data ? row.user_data.username : '已删除';
                            },
                            visible: !Config.userId,
                            searchable: !Config.userId
                        },
                        {
                            field: 'amount_gain',
                            title: __('充值金额'),
                            sortable: true,
                            operate: false,
                        },
                        {
                            field: 'amount_left',
                            title: __('剩余金额'),
                            sortable: true,
                            operate: false,
                        },
                        {
                            field: 'amount_use',
                            title: __('花费金额'),
                            sortable: true,
                            operate: false,
                        },
                        {
                            field: 'amount_back',
                            title: __('退回金额'),
                            sortable: true,
                            operate: false,
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: Config.statusList,
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'updated_at',
                            title: __('Update time'),
                            sortable: true,
                            operate: false,
                            formatter: Table.api.formatter.datetime,
                        },
                        {
                            field: 'created_at',
                            title: __('Create time'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange'
                        },
                        // 操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
                        {
                            field: 'operate',
                            width: '150px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'order',
                                    text: __('订单'),
                                    title: function (row) {
                                        return __('充值订单') + ' - ' + (row.user_data ? row.user_data.username : '已删除');
                                    },
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    icon: 'fa fa-list-alt',
                                    url: function (row) {
                                        return 'yunfood/fund/order/index?user_id=' + row.user_id;
                                    },
                                },
                                {
                                    name: 'record',
                                    text: __('记录'),
                                    title: function (row) {
                                        return __('账户流水') + ' - ' + (row.user_data ? row.user_data.username : '已删除');
                                    },
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    icon: 'fa fa-list',
                                    url: function (row) {
                                        return 'yunfood/fund/record/index?user_id=' + row.user_id;
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        },
                    ],
                ],
                // 快捷搜索,这里可在控制器定义快捷搜索的字段
                search: false,
                // 启用普通表单搜索
                commonSearch: true,
                // 显示切换视图（table/card）按钮
                showToggle: false,
                // 显示导出按钮
                showExport: true,
                // 启用跨页选择
                maintainSelected: true,
                // 启用固定列
                fixedColumns: true,
                // 固定右侧列数
                fixedRightNumber: 1,
                // 可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: true,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {  // 渲染的方法
            },
            events: { // 绑定事件的方法
            }
        }
    };
    return Controller;
});
